﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using LSA.Framework;

namespace Visuals
{
    /// <summary>
    /// Interaction logic for CoordinateChart.xaml
    /// </summary>
    public partial class CoordinateChartWindow : Window
    {
        public List<Ellipse> Points;
        private Dictionary<Ellipse, int> ids = new Dictionary<Ellipse, int>();
        private int xConst = 155;
        private int yConst = 155;

        public CoordinateChartWindow(List<WordInfo> pairs)
        {
            InitializeComponent();

            firstDimention.Maximum = MainWindow.Object.lsaSpace.Dimension - 1;
            secondDimention.Maximum = MainWindow.Object.lsaSpace.Dimension - 1;

            foreach (WordInfo word in pairs)
            {
                var a = new Ellipse {Fill = Brushes.DarkGreen, Opacity = 0.7, Height = 3, Width = 3};
                a.SetValue(Canvas.LeftProperty, MainWindow.Object.lsaSpace.GetWordCoordinate(word.Id, (int) firstDimention.Value) + xConst);
                a.SetValue(Canvas.BottomProperty, MainWindow.Object.lsaSpace.GetWordCoordinate(word.Id, (int)secondDimention.Value) + yConst);
                a.ToolTip = word.Word;
                a.MouseEnter += a_MouseEnter;
                a.MouseLeave += a_MouseLeave;
                canvas.Children.Add(a);
                ids[a] = word.Id;
            }
        }

        private void a_MouseEnter(object sender, MouseEventArgs args)
        {
            var obj = ((Ellipse) sender);
            obj.Fill = Brushes.Indigo;
            obj.Opacity = 0.8;
            wordName.Text = (string) obj.ToolTip;
            wordCoords.Text = "X: " + (((double) obj.GetValue(Canvas.LeftProperty)) - xConst) + ", Y: " +
                              (((double) obj.GetValue(Canvas.BottomProperty)) - yConst);
        }

        private void a_MouseLeave(object sender, MouseEventArgs args)
        {
            var obj = ((Ellipse) sender);
            obj.Fill = Brushes.Green;
            obj.Opacity = 0.5;
        }

        private void Animate()
        {
            try
            {
                foreach (var child in canvas.Children)
                {
                    var a = child as Ellipse;
                    if (a != null)
                    {
                        DoubleAnimation animationX = new DoubleAnimation
                                                         {
                                                             From = (double)a.GetValue(Canvas.LeftProperty),
                                                             To =
                                                                 MainWindow.Object.lsaSpace.GetWordCoordinate(ids[a], (int)firstDimention.Value) +
                                                                 xConst,
                                                             Duration = new Duration(TimeSpan.FromSeconds(0.5))
                                                         };
                        DoubleAnimation animationY = new DoubleAnimation
                                                         {
                                                             From = (double)a.GetValue(Canvas.BottomProperty),
                                                             To =
                                                                 MainWindow.Object.lsaSpace.GetWordCoordinate(ids[a], (int)secondDimention.Value) +
                                                                 yConst,
                                                             Duration = new Duration(TimeSpan.FromSeconds(0.5))
                                                         };
                        a.BeginAnimation(Canvas.LeftProperty, animationX);
                        a.BeginAnimation(Canvas.BottomProperty, animationY);
                    }
                }
            }
            catch (ArgumentOutOfRangeException)
            {
                MessageBox.Show("Не можете да измествате измерението повече в тази посока.");
            }
        }

        private void ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            Animate();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            foreach (var child in canvas.Children)
            {
                var a = child as Ellipse;
                if(a != null)
                {
                    a.SetValue(Canvas.LeftProperty, ((double) a.GetValue(Canvas.LeftProperty)) * 1.5);
                    a.SetValue(Canvas.BottomProperty, ((double)a.GetValue(Canvas.BottomProperty)) * 1.5);
                }
            }
        }
    }
}
